package cqrtplm.listener;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainer;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import cqrtplm.service.RTReviseObjectByEcnService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.List;

@CustomListener(
        category = "润通",
        description = "自动修订ECA流程的变更对象并关联到ECA流程"
)
public class RTReviseChgBeforeObjectByEcaListener implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(RTReviseChgBeforeObjectByEcaListener.class);
    private static final long serialVersionUID = 5204846924354832635L;

    public void notify(DelegateExecution delegateExecution) {
        try {
            log.info(
                    "开始执行监听 ==> 自动修订ECA流程的变更对象并关联到ECA流程RTReviseChgBeforeObjectByEcaListener");
            String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
            if (CharSequenceUtil.isBlank(workFlowContainerId)) {
                workFlowContainerId = String.valueOf(delegateExecution.getVariable("workFlowContainerId"));
            }

            TyplmWorkFlowContainerService typlmWorkFlowContainerService = (TyplmWorkFlowContainerService) SpringUtil.getBean(
                    TyplmWorkFlowContainerService.class);
            WorkFlowContainer workFlowContainer = (WorkFlowContainer) typlmWorkFlowContainerService.selectOneByOid(
                    new BigInteger(workFlowContainerId));
            BigInteger ecaId = workFlowContainer.getBaseObjectOid();
            RTReviseObjectByEcnService rtReviseObjectByEcnService = (RTReviseObjectByEcnService) SpringUtil.getBean(
                    RTReviseObjectByEcnService.class);
            rtReviseObjectByEcnService.reviseChgBeforeObjectByEca(ecaId, (List) null);
            log.info(
                    "正常执行监听结束 <== 自动修订ECA流程的变更对象并关联到ECA流程RTReviseChgBeforeObjectByEcaListener");
        } catch (Exception e) {
            log.error("监听RTReviseChgBeforeObjectByEcaListener异常：", e);
            throw e;
        }
    }
}
